/*
 * @Description: 
 * @Author: duan_shuangjun 丶
 * @Date: 2025-09-17 15:45:39
 * @LastEditTime: 2025-09-17 17:15:35
 */
import { ok, fail } from '../utils/response'
import storeShopDeskRepository from '../database/repositories/storeShopDeskRepository'
import storeShopDeskOrderRepository, { StoreShopDeskOrderRow } from '../database/repositories/storeShopDeskOrderRepository'
import storeShopDeskRecordRepository, { StoreShopDeskRecordRow } from '../database/repositories/storeShopDeskRecordRepository'
import storeShopDeskOrderFoodRepository, { StoreShopDeskOrderFoodRow } from '../database/repositories/storeShopDeskOrderFoodRepository'
import type { StoreShopDesk } from '../database/types'

/**
 * 桌台域服务（仅保留“批量新增/更新”能力）
 * - 面向数据同步/初始化场景，将后端数据批量落到本地 SQLite
 * - 不包含查询、删除等复杂业务，后续按需求另开方法
 */
export class DeskDomainService {
    /**
     * 桌台数据 批量新增/更新
     * @param desks 后端返回的桌台列表（包含主键 id）
     * @returns ApiResponse<boolean>
     */
    static deskBatchUpsert(desks: StoreShopDesk[]) {
        try {
            storeShopDeskRepository.upsertBatch(desks)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'desk batch upsert failed')
        }
    }

    /**
     * 桌台订单 批量新增/更新
     * @param rows 订单列表（包含主键 id、order_no 等）
     * @returns ApiResponse<boolean>
     */
    static orderBatchUpsert(rows: StoreShopDeskOrderRow[]) {
        try {
            storeShopDeskOrderRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'order batch upsert failed')
        }
    }

    /** 桌台使用记录 批量新增/更新 */
    static recordBatchUpsert(rows: StoreShopDeskRecordRow[]) {
        try {
            storeShopDeskRecordRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            console.error(e)
            return fail(e?.message || 'record batch upsert failed')
        }
    }

    /** 桌台订单菜品 批量新增/更新 */
    static orderFoodBatchUpsert(rows: StoreShopDeskOrderFoodRow[]) {
        try {
            storeShopDeskOrderFoodRepository.upsertBatch(rows)
            return ok(true)
        } catch (e: any) {
            return fail(e?.message || 'order food batch upsert failed')
        }
    }
}

export default DeskDomainService


